use crate::app::handle::login;
use crate::app::server::*;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use crate::app::handle::room_handle;

pub fn init_call_register(server: &mut entity::ServerEntity) {
    server.regist_call(
        1,
        Arc::new(HandleOne {
            totil: AtomicU64::new(0),
        }),
    );

    server.regist_call(login::LOGIN, Arc::new(login::LoginOrRegist {}));
    server.regist_call(login::REGISTR, Arc::new(login::LoginOrRegist {}));
    server.regist_call(login::VALIDATION, Arc::new(login::LoginOrRegist {}));

    server.regist_call(room_handle::GETROOMINFOLIST,Arc::new(room_handle::HandleRoom{}));
    server.regist_call(room_handle::JOINROOM,Arc::new(room_handle::HandleRoom{}));
    server.regist_call(room_handle::ROOMMSG,Arc::new(room_handle::HandleRoom{}));
}

pub struct HandleOne {
    totil: AtomicU64,
}

impl handle::Handle for HandleOne {
    fn handle(
        &self,
        client: &mut conn::ConnEntity,
        msg: message::Message,
    ) -> (bool, message::Message) {
        self.totil.fetch_add(1, Ordering::Relaxed);
        let ctx = msg.body.clone();
        log_debug_ln!(
            "client({}:{}):{}",
            client.addr.ip(),
            client.addr.port(),
            String::from_utf8_lossy(ctx.as_slice())
        );
        (true, msg)
    }
}
